<?php
header("Content-type:text/html;charset=utf-8");
class BaseController extends Controller {

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}


	/**
	 * @Author:      lyt
	 * @DateTime:    2016-01-04 17:14:34
	 * @Description: 用于 验证scs用户token的 公用方法
	 */
	protected function verificationSCSUserToken($token,&$response){
	$sc_token = ScsUserToken::getToken($token);
        if(!$sc_token){
            $response = Lang::get("scs_error.user_token_not_exist_21006");
            return null;
        }

        //验证token shijian 


        //验证用户
        $sc_user=SCUser::find($sc_token->sc_user_id);
        if (is_null($sc_user)) {
            $response = Lang::get("scs_error.scs_user_not_exiet21009");
            return null;
        }
        if ($sc_user->status == 2) { //冻结用户
            $response = Lang::get("scs_error.scs_user_frozen_error_21011");
            return null;
        }
        if ($sc_user->status == 3) { //用户删除
            $response = Lang::get("scs_error.scs_user_deletet_error_21010");
            return null;
        }
        if ($sc_user->status != 1) { //用户删除
            $response = Lang::get("scs_error.scs_user_unknown_error_21012");
            return null;
        }
        $ms=MaintenanceShops::find($sc_user->shop_id);
        if(is_null($ms)){
            $response = Lang::get("scs_error.scs_user_shop_error_21013");
            return null;
        }
        if($ms->status!=1){
            $response = Lang::get("scs_error.scs_user_shop_status_error_21014");
            return null;
        }
        
        $sc_user->shop=$ms;
        return $sc_user;
	}


    /**
     * @Author:dx
     * @DateTime:    2016-02-16 17:14:34
     * @Description: 用于 验证mkt用户token的 公用方法
     */
    protected function verificationMKTUserToken($token,&$response){
        $mkt_token = MktUserToken::getToken($token);
        if(!$mkt_token){
            $response = Lang::get("mkt_error.user_token_not_exist_210006");
            return null;
        }

        //验证用户
        $mkt_user=MktUser::find($mkt_token->mkt_user_id);
        if (is_null($mkt_user)) {
            $response = Lang::get("mkt_error.mkt_user_not_exiet210009");
            return null;
        }
        if ($mkt_user->status == 2) { //冻结用户
            $response = Lang::get("mkt_error.mkt_user_frozen_error_210011");
            return null;
        }
        if ($mkt_user->status == 3) { //用户删除
            $response = Lang::get("mkt_error.mkt_user_deletet_error_210010");
            return null;
        }
        if ($mkt_user->status != 1) { //用户删除
            $response = Lang::get("mkt_error.mkt_user_unknown_error_210012");
            return null;
        }
        return $mkt_user;
    }
    
    /**
     * @Author:zwh
     * @DateTime:    2016-12-26 17:04:43
     * @Description: 用于 验证钣金业务用户token的 公用方法
     */
    protected function verificationMaintenanceEngineerToken($token,&$response){
        $me_token = MeUserToken::where('token',$token)->first();
        if(!$me_token){
            $response = Lang::get("scs_error.user_token_not_exist_21006");
            return null;
        }
    
        //验证用户
        $me=MaintenanceEngineers::find($me_token->me_id);
        if (is_null($me)) {
            $response = Lang::get("scs_error.scs_user_not_exiet21009");
            return null;
        }
        if ($me->status != 1) { //用户删除
            $response = Lang::get("scs_error.scs_user_unknown_error_21012");
            return null;
        }
        return $me;
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-02-22 14:14:34
     * @Description: 用于 验证wechat用户token的 公用方法
     */
    protected function verificationUserToken($token,&$response){
        $wechat_token = UserTokens::getToken($token);
        if(!$wechat_token){
            $response = Lang::get("error.user_token_not_exist_21000");
            return null;
        }
        //验证用户
        $wechat_user=Users::find($wechat_token->user_id);
        if (is_null($wechat_user)) {
            $response = Lang::get("error.user_not_exit_21001");
            return null;
        }
        if ($wechat_user->status == 2) { //冻结用户
            $response = Lang::get("error.user_frozen_error_21002");
            return null;
        }
        if ($wechat_user->status == 3) { //用户删除
            $response = Lang::get("error.user_deletet_error_21003");
            return null;
        }
        if ($wechat_user->status != 1) { //用户删除
            $response = Lang::get("error.user_unknown_error_21004");
            return null;
        }
        return $wechat_user;
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-02-23 13:58:34
     * @Description: 用于 验证安慧车服后台用户token的 公用方法
     */
    protected function verificationAhpUserToken($token,&$response){
        $ahp_token = AhpUserTokens::getToken($token);
        if(!$ahp_token){
            $response = Lang::get("ahp_error.user_token_not_exist_2100");
            return null;
        }
        //验证用户
        $ahp_user=AhpUsers::find($ahp_token->ahp_user_id);
        if (is_null($ahp_user)) {
            $response = Lang::get("ahp_error.user_not_exit_2101");
            return null;
        }
        if ($ahp_user->status == 2) { //冻结用户
            $response = Lang::get("ahp_error.user_frozen_error_2102");
            return null;
        }
        if ($ahp_user->status == 3) { //用户删除
            $response = Lang::get("ahp_error.user_deletet_error_2103");
            return null;
        }
        if ($ahp_user->status != 1) { //用户删除
            $response = Lang::get("ahp_error.user_unknown_error_2104");
            return null;
        }
        return $ahp_user;
    }

    /**
     * @Author: ZH
     * @DateTime:    2016-02-16 17:14:34
     * @Description: 用于 验证pms用户token的 公用方法
     */
    protected function verificationPMSUserToken($token,&$response){
        $res = ['user'=>null,'status'=>0,'response'=>$response];
        $PMS_token = PMSUserToken::getToken($token);
        if(!$PMS_token){
            $res['response'] = Lang::get("pms_error.user_token_not_exist_210006");
            return $res;
        }
        //验证用户
        $pms_user = PMSUser::find($PMS_token->pms_user_id);
        if (is_null($pms_user)) {
            $res['response'] = Lang::get("pms_error.user_not_exist_210009");
            return $res;
        }
        $res['user'] = $pms_user;
        if ($pms_user->status == 2) { //冻结用户
            $res['response'] = Lang::get("pms_error.user_frozen_error_210011");
        }
        if ($pms_user->status == 3) { //用户删除
            $res['response'] = Lang::get("pms_error.user_deletet_error_210010");
        }
        if (!in_array($pms_user->status,[1,2,3])) { //未知
            $res['response'] = Lang::get("pms_error.user_unknown_error_210012");
        }
        $res['status'] = 1;
        return $res;
    }
    
    /**
     * @Author: zk
     * @DateTime:    18-7-12 上午10:49
     * @Description: 用于 验证OA系统用户token的 公用方法
     * @param string $token 待校验token
     * @param array $response 状态数组
     * @return array
     */
    protected function verificationOAUserToken($token, &$response){
        $oa_token = OAUserToken::getToken($token);
        if(!$oa_token){
            $response = Lang::get('oa_error.user_token_not_exist_210006');
            return null;
        }
        //验证用户
        $oa_user = OAUser::find($oa_token->oa_user_id);
        if (is_null($oa_user)) {
            $response = Lang::get('oa_error.user_not_exist_210007');
            return null;
        }
        if ($oa_user->status == 2) { //冻结用户
            $response = Lang::get('oa_error.login_user_status_error_210002');
            return null;
        }
        if ($oa_user->status == 3) { //用户删除
            $response = Lang::get('oa_error.login_user_status_error_210004');
            return null;
        }
        if ($oa_user->status != 1) { //用户状态异常
            $response = Lang::get('oa_error.user_unknown_error_210008');
            return null;
        }
        return $oa_user;
    }

    protected function verificationASSUserToken($token, &$response){
        $ass_user_token = ASSUserToken::getToken($token);
        if(!$ass_user_token){
            $response = Lang::get('ass_error.args_error_100003');
            return null;
        }
        //验证用户
        $ass_user = ASSUser::find($ass_user_token->ass_user_id);
        if (is_null($ass_user)) {
            $response = Lang::get('ass_error.args_error_100001');
            return null;
        }
        $ass_user_info = MaintenanceSupplier::find($ass_user->pms_supplier_id);
        if (is_null($ass_user_info)) {
            $response = Lang::get('ass_error.args_error_100001');
            return null;
        }
        if ($ass_user_info->work_together_type_ass == 1) { //冻结用户
            $response = Lang::get('ass_error.login_user_status_error_210001');
            return null;
        }
        if ($ass_user_info->work_together_type_ass == 2) { //用户删除
            $response = Lang::get('ass_error.login_user_status_error_210002');
            return null;
        }
        if ($ass_user_info->work_together_type_ass == 3) { //用户状态异常
            $response = Lang::get('ass_error.login_user_status_error_210003');
            return null;
        }
        if (!in_array($ass_user_info->work_together_type,  [1, 2, 3])) { //用户状态异常
            $response = Lang::get('ass_error.login_user_status_error_210004');
            return null;
        }
        $ass_user_info->ass_user_id = $ass_user->id;
        $ass_user_info->token = $ass_user_token->token;
        $ass_user_info->password = $ass_user->password;
        return $ass_user_info;
    }
    
}
